<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 * 
 */
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Session', 'Js','Javascript','Ajax');
var $components = array('Session','Auth','Cookie');
var $uses = array('User', 'Nhanvien', 'Chucdanh','Role','Donvi','Thuocdv');

 function beforeFilter(){
        	$this->Auth->authenticate = ClassRegistry::init('User');
            $user = $this->Auth->user();
            
            if(empty($user) && $this->params['action'] != 'login'){
			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		      }
       	$this->Auth->loginAction 	= array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect 	= array('controller'=>'pages', 'action' => 'home');
		$this->Auth->loginError 	= 'Tên tài khoản hoặc mật khẩu sai';
		$this->Auth->authError 		= 'Bạn cần phải đăng nhập hệ thống để sử dụng các chức năng';
		$this->Auth->logoutRedirect = '/';      
   }
   
 function beforeRender() {
		
        // Tạo biến curUser trong session
        $curUser=$this->Auth->user();
    
        if(count($curUser)>0){
            $hoten= $this->User->query("SELECT nhanviens.tennv, nhanviens.id, chucdanhs.chucdanh, donvis.tendv,donvis.id, donvis.madv 
                        from nhanviens, users,chucdanhs,donvis 
                            where nhanviens.id=users.nhanvien_id and nhanviens.chucdanh_id=chucdanhs.id
                            and nhanviens.donvi_id=donvis.id
                             and users.id=".$curUser['User']['id']);
                   
            //print_r($donvi_cha);
            $quyen= $this->User->query("SELECT phanquyens.role_id
                                        from chucdanhs, phanquyens, nhanviens, users
                                       where chucdanhs.id=phanquyens.chucdanh_id
                                       and chucdanhs.id=nhanviens.chucdanh_id
                                       and nhanviens.id=users.nhanvien_id
                                       and users.id=".$curUser['User']['id']);
            
             $nv_id=$hoten[0]['nhanviens']['id'];
             $iddv=$hoten[0]['donvis']['id'];
             $tendvs=$hoten[0]['donvis']['madv'];
             //print_r($tendv);
            $this->Session->write('cur_dv',$iddv);
            $this->Session->write('cur_tendv',$tendvs);
            $this->Session->write('cur_role',$quyen);
            $role_id=$this->Session->read('cur_role');
            $this->Session->write('cur_manv',$nv_id);
            if($iddv > 1) {
                $donvi_cha=$this->User->query("select donvi_id from thuocdvs where donvi=".$iddv);
                //print_r($donvi_cha);
                $idcha=$donvi_cha[0]['thuocdvs']['donvi_id'];
                 $this->Session->write('cur_cha',$idcha);
            }
        }
      
// 
        $this->set(compact('curUser','hoten','iddv','quyen','role_id','nv_id','idcha','tendv'));
        
	}


 /**
 * Hinhanhdb
 * @var Hinhanhdb Model
 */
 var $Hinhanhdb;
/**
 * thongtindb
 * @var Thongtindb Model
 */
 var $Thongtindb;
/**
 * Thongbao
 * @var Thongbao Model
 */
 var $Thongbao;
/**
 * Ket qua ban hang trung tam
 * @var Kqbanhangtt Model
 */
 var $Kqbanhangtt;
/**
 * Luy ke tuan nhan vien 
 *
 * @var Luyke Model
 */
 var $Zluykenv;
/**
 * Chi Tieu Don vi 
 *
 * @var Chitieudv Model
 */
 var $Chitieudv;
 /**
 * Chi Tieu Nhan Vien 
 *
 * @var Chitieunv Model
 */
 var $Chitieunv;
 /**
 * Chuc Danh
 *
 * @var Chucdanh Model
 */
 var $Chucdanh;
 /**
 * Diem Chuan
 *
 * @var Diemchuan Model
 */
 var $Diemchuan;
 /**
 * Don vi
 *
 * @var Donvi Model
 */
 var $Donvi;
 /**
 * Don Vi Tinh
 *
 * @var Dvt Model
 */
 var $Dvt;
 /**
 * Hang hoa
 *
 * @var Hanghoa Model
 */
 var $Hanghoa;
 /**
 * Ha Tang
 *
 * @var Hatang Model
 */
 var $Hatang;
 /**
 * Ke Hoach
 *
 * @var Kehoach Model
 */
 var $Kehoach;
 /**
 * Kenh 
 *
 * @var Kenh Model
 */
 var $Kenh;
 /**
 * Ket Qua Ban Hang
 *
 * @var Kqbanhang Model
 */
 var $Kqbanhang;
 /**
 * Loai
 *
 * @var Loai Model
 */
 var $Loai;
 /**
 * Loai Hang
 *
 * @var Loaihang Model
 */
 var $Loaihang;
 /**
 * Loai Kenh
 *
 * @var Loaikenh Model
 */
 var $Loaikenh;
 /**
 * Nhan Vien
 *
 * @var Nhanvien Model
 */
 var $Nhanvien;
 /**
 * Cong Viec Theo Chi Thi
 *
 * @var Nvchithi Model
 */
 var $Nvchithi;
 
 /**
 * Cong Viec Theo Ngay
 *
 * @var Nvngay Model
 */
 var $Nvngay;
 /**
 * Phan kenh
 *
 * @var Phankenh Model
 */
 var $Phankenh;
 /**
 * Phan Quyen
 *
 * @var Phanquyen
 */
 var $Phanquyen;
 /**
 * Role
 *
 * @var Role
 */
 var $Role;
 /**
 * Thoi Gian
 *
 * @var Thoigian
 */
 var $Thoigian;
 /**
 * Thuoc Don Vi
 *
 * @var Thuocdv
 */
 var $Thuocdv;
 /**
 * Chitieukenh
 *
 * @var Chitieukenh
 */
 var $Chitieukenh;
 
 /**
 * Hanghoabhld
 *
 * @var Hanghoabhld
 */
 var $Hanghoabhld;
 /**
 * Kehoachnv
 *
 * @var Kehoachbhld
 */
 var $Kehoachbhld;
 /**
 * Kehoachnv
 *
 * @var Chitieungay
 */
 var $Chitieungay;
 /**
 * Kehoachnv
 *
 * @var Nhansubhld
 */
 var $Nhansubhld;
 /**
 * Kehoachnv
 *
 * @var Kehoachnv
 */
 var $Kehoachnv;
 
/**
 * Thitruongkenh
 *
 * @var Thitruongkenh
 */
 var $Thitruongkenh;
 
/**
 * Chamsockenh
 *
 * @var Chamsockenh
 */
 var $Chamsockenh;
 
 /**
 * Z1thongbaos
 *
 * @var Z1thongbao
 */
 var $Z1thongbao;
 /**
 * Congtackenh
 *
 * @var Congtackenh
 */
 var $Congtackenh;
 /**
 * User
 *
 * @var User
 */
 var $User;
 
 
 /**
 * AuthComponent
 *
 * @var AuthComponent
 */
 var $Auth;
 
 /**
 * SessionComponent
 *
 * @var SessionComponent
 */
 var $Session;
 
  /**
 * RequestHandlerComponent
 *
 * @var RequestHandlerComponent
 */
  var $RequestHandler;
}
